import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jump_scape/js_data/js_box/js_init.dart';
import 'package:jump_scape/js_data/js_login.dart';
import 'package:jump_scape/js_ui/js_activity/js_login.dart';
import 'package:jump_scape/js_ui/js_dialog/js_setting.dart';
import 'package:jump_scape/js_ui/js_widget/js_image_view.dart';
import 'package:jump_scape/js_ui/js_widget/js_style.dart';

import '../../js_config.dart';
import '../../js_data/js_event.dart';
import '../../objectbox.g.dart';
import '../js_dialog/js_loading.dart';

class JsSettingActivity extends StatefulWidget {
  static const url = 'setting';
  const JsSettingActivity({super.key});

  @override
  State<StatefulWidget> createState() => _JsSettingActivity();
}

class _JsSettingActivity extends State<JsSettingActivity> {
  bool _isAutoTranslate = false;
  String _userId = '';

  @override
  void initState() {
    super.initState();
    JsEventManager.statPage(page: JsEventName.jsNameSetting);
    _init();
  }

  _init() async {
    final setting = await JsObjectBox.setting;
    var member = JsObjectBox.memberBox.query(MemberBox_.userId.equals(setting.userId)).build().findFirst();
    if (member != null) {
      _userId = member.userId;
      _isAutoTranslate = member.isAutoTranslate;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(false),
      child: Scaffold(
        body: Stack(
          children: [
            jsImage('assets/image/ic_records_bg.png', width: double.infinity, height: double.infinity, fit: BoxFit.cover),
            Padding(
              padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
              child: Row(
                children: [
                  InkWell(
                    onTap: () {
                      if (Navigator.canPop(context)) {
                        Navigator.pop(context);
                      }
                    },
                    child: Container(
                      padding: EdgeInsets.fromLTRB(18.r, 15.r, 18.r, 0),
                      child: jsImage('assets/image/ic_finish.png', width: 64.r, height: 64.r),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              height: double.infinity,
              margin: EdgeInsets.fromLTRB(26.r, ScreenUtil().statusBarHeight + 40.r, 26.r, 0),
              clipBehavior: Clip.antiAlias,
              decoration: jsShapeRoundT(radius: 200.r, color: Colors.white),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    jsImage('assets/image/ic_setting.png', width: 70.r, fit: BoxFit.fitWidth, padding: EdgeInsets.only(top: 28.r)),
                    Container(
                      margin: EdgeInsets.only(top: 34.r, bottom: 9.r),
                      decoration: jsShapeRound(radius: 14.r, borderColor: Colors.black, borderWidth: 2.r),
                      child: Container(
                        width: 81.r,
                        height: 81.r,
                        clipBehavior: Clip.antiAlias,
                        decoration: jsShapeRound(radius: 14.r),
                        child: jsImage('assets/image/ic_logo.png'),
                      ),
                    ),
                    Container(
                      height: 38.r,
                      alignment: Alignment.center,
                      child: Text(JsApp.appName, style: jsTextStyle(font: JsTextFont.PoetsenOne, fontColor: Colors.black, fontSize: 18.r)),
                    ),
                    InkWell(
                      child: Container(
                        height: 45.r,
                        padding: EdgeInsets.fromLTRB(13.r, 0, 22.r, 0),
                        margin: EdgeInsets.fromLTRB(18.r, 30.r, 18.r, 0),
                        decoration: jsShapeRound(radius: 10.r, color: Color(0xFFF9F9F9)),
                        child: Row(
                          children: [
                            Text('Id：$_userId', style: jsTextStyle(font: JsTextFont.October_Devanagari_Regular, fontColor: Colors.black, fontSize: 16.r)),
                            Spacer(),
                            Container(
                              clipBehavior: Clip.antiAlias,
                              decoration: jsShapeRound(radius: 4.r, shadows: [
                                BoxShadow(offset: Offset(1.r, 1.r), blurRadius: 2.r, color: Color(0xFFC9C9C9)),
                              ]),
                              child: jsImage('assets/image/ic_personal_copy.png', width: 26.r, fit: BoxFit.fitWidth),
                            ),
                          ],
                        ),
                      ),
                      onTap: () async {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                            content: Text('Copy success', style: jsTextStyle(font: JsTextFont.PoetsenOne, fontColor: Colors.white, fontSize: 20.r)),
                          ),
                        );

                        await Clipboard.setData(ClipboardData(text: _userId));
                      },
                    ),
                    // InkWell(
                    //   child: Container(
                    //     width: double.infinity,
                    //     height: 45.r,
                    //     padding: EdgeInsets.fromLTRB(13.r, 0, 13.r, 0),
                    //     margin: EdgeInsets.fromLTRB(18.r, 30.r, 18.r, 0),
                    //     alignment: Alignment.centerLeft,
                    //     decoration: jsShapeRound(radius: 10.r, color: Color(0xFFF9F9F9)),
                    //     child: Row(
                    //       children: [
                    //         Text("Auto Translate", style: jsTextStyle(font: JsTextFont.October_Devanagari_Regular, fontColor: Colors.black, fontSize: 16.r)),
                    //         Spacer(),
                    //         Switch(
                    //           value: _isAutoTranslate,
                    //           onChanged: ((isAutoTranslate) async {
                    //             if (_isAutoTranslate != isAutoTranslate) {
                    //               final setting = await JsObjectBox.setting;
                    //               var member = JsObjectBox.memberBox.query(MemberBox_.userId.equals(setting.userId)).build().findFirst();
                    //               if (member != null) {
                    //                 member.isAutoTranslate = isAutoTranslate;
                    //                 JsObjectBox.memberBox.put(member);
                    //               }

                    //               _isAutoTranslate = isAutoTranslate;
                    //               setState(() {});
                    //             }
                    //           }),
                    //           activeColor: Colors.white,
                    //           activeTrackColor: Color(0xFF20C7F6),
                    //           inactiveThumbColor: Colors.white,
                    //           inactiveTrackColor: Color(0xFFCCCCCC),
                    //         ),
                    //       ],
                    //     ),
                    //   ),
                    //   onTap: () {},
                    // ),
                    InkWell(
                      child: Container(
                        width: double.infinity,
                        height: 45.r,
                        padding: EdgeInsets.fromLTRB(13.r, 0, 13.r, 0),
                        margin: EdgeInsets.fromLTRB(18.r, 30.r, 18.r, 0),
                        alignment: Alignment.centerLeft,
                        decoration: jsShapeRound(radius: 10.r, color: Color(0xFFF9F9F9)),
                        child: Text("Log Out", style: jsTextStyle(font: JsTextFont.October_Devanagari_Regular, fontColor: Colors.black, fontSize: 16.r)),
                      ),
                      onTap: () {
                        JsEventManager.statPage(page: JsEventName.jsNameLogout);

                        jsShowLoadingDialog(context);

                        JsLogin().logout(onSuccess: () {
                          Navigator.pushNamedAndRemoveUntil(context, JsLoginActivity.url, (route) {
                            return false;
                          });
                        }, onFailed: (msg) {
                          Navigator.pop(context);
                          if (msg.isNotEmpty) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text(msg, style: jsTextStyle(font: JsTextFont.PoetsenOne, fontColor: Colors.white, fontSize: 20.r)),
                              ),
                            );
                          }
                        });
                      },
                    ),
                    InkWell(
                      child: Container(
                        width: double.infinity,
                        height: 45.r,
                        padding: EdgeInsets.fromLTRB(13.r, 0, 13.r, 0),
                        margin: EdgeInsets.fromLTRB(18.r, 30.r, 18.r, 0),
                        alignment: Alignment.centerLeft,
                        decoration: jsShapeRound(radius: 10.r, color: Color(0xFFF9F9F9)),
                        child: Text("Delete Account", style: jsTextStyle(font: JsTextFont.October_Devanagari_Regular, fontColor: Colors.black, fontSize: 16.r)),
                      ),
                      onTap: () {
                        jsShowDeleteAccountDialog(context, (isDelete) {
                          if (isDelete) {
                            JsEventManager.statPage(page: JsEventName.jsNameDeleteaccount);

                            jsShowLoadingDialog(context);

                            JsLogin().deleteAccount(onSuccess: () {
                              Navigator.pushNamedAndRemoveUntil(context, JsLoginActivity.url, (route) {
                                return false;
                              });
                            }, onFailed: (msg) {
                              Navigator.pop(context);
                              if (msg.isNotEmpty) {
                                ScaffoldMessenger.of(context).showSnackBar(
                                  SnackBar(
                                    content: Text(msg, style: jsTextStyle(font: JsTextFont.PoetsenOne, fontColor: Colors.white, fontSize: 20.r)),
                                  ),
                                );
                              }
                            });
                          }
                        });
                      },
                    ),
                    SizedBox(height: 224.r),
                  ],
                ),
              ),
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: IgnorePointer(
                child: jsImage('assets/image/ic_home_bg_bottom.png', width: double.infinity, fit: BoxFit.fitWidth),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
